<?php


namespace App\Models\CacheModel\active;


use app\common\lib\exception\ApiException;
use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Active1UserHelpRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.active1_user_help_record';

    protected $cacheModel = 'active.help_recode';

    /**
     * 添加助力记录
     * @param array $data
     * @return bool
     */
    public function SaveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            $this->listDataAdd($data['help_id'], [$id => $id]);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('user_help_id', $data['active_flag'] . ':' . $data['uid'])->setCache([$data['help_id'] => $data['date']], 'user_help_id');
            return true;
        }
        catch (\Exception $e)
        {
            Log::write('Active1UserHelpRecord model => SaveData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

            return false;
        }
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string
     * @throws ApiException
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $where = [];
            if ($ids)
            {
                $where = ['r.id' => ['in', $ids]];
            } else
            {
                $where['r.help_id'] = $type[0];
            }

            return $this->alias('r')->where($where)->join('wechat_backup_07_18_14_43_00.app_user u', 'r.uid=u.id')->order('r.id ' . $sortType)->column('u.nickname,r.date,r.create_time', 'r.id');
        }
        catch (\Exception $e)
        {
            return [];
        }
    }

    /**
     * 获取助力列表
     * @param string $helpId
     * @return array
     * @throws \Exception
     */
    public function getHelpList(string $helpId): array
    {
        request()->get(['page' => -1]);
        $pageData = $this->getListPage($helpId, 'id', [], 0, 'desc', 0);
        $resData = [];

        foreach ($pageData['data'] as $datum)
        {
            $resData[] = "好友 {$datum['nickname']} 完成助力";
        }

        return $resData;
    }

    /**
     * 获取 用户助力的 助力ID
     * @param int $activeFlag
     * @param int $uid
     * @throws \Exception
     */
    public function getUserHelpIds(int $activeFlag, int $uid)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('user_help_id', $activeFlag . ':' . $uid)
            ->getDataAndSet(function () use ($activeFlag, $uid)
            {
                try
                {
                    return $this->where(['active_flag' => $activeFlag, 'uid' => $uid])->column('date', 'help_id');
                }
                catch (\Exception $e)
                {
                    Log::write('Active1UserHelp model => getUserHelpIds()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
                    return [];
                }
            }, [], 'user_help_id');

        return $data;
    }
}
